package com.sweets.look.ui.browser;

import android.app.DownloadManager;
import android.content.Context;
import android.os.Environment;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class FetchUrlMimeType extends Thread {
    private final Context mContext;
    private final String mCookies;
    private final DownloadManager.Request mRequest;
    private final String mUri;
    private final String mUserAgent;

    public FetchUrlMimeType(Context context, DownloadManager.Request request, String uri, String cookies,
                            String userAgent, String fileName) {
        super();
        this.mContext = context.getApplicationContext();
        this.mRequest = request;
        this.mUri = uri;
        this.mCookies = cookies;
        this.mUserAgent = userAgent;
    }

    public void run() {
        String v3;
        HttpURLConnection connection = null;
        String v8 = null;
        String v2 = null;
        try {
            connection = (HttpURLConnection) new URL(this.mUri).openConnection();
            if (this.mCookies != null && !this.mCookies.isEmpty()) {
                connection.addRequestProperty("Cookie", this.mCookies);
                connection.setRequestProperty("User-Agent", this.mUserAgent);
            }

            connection.connect();
            if (connection.getResponseCode() == 200) {
                String contentType = connection.getHeaderField("Content-Type");
                if (contentType != null) {
                    v8 = contentType;
                    int v10 = v8.indexOf(59);
                    if (v10 != -1) {
                        v8 = v8.substring(0, v10);
                    }
                }

                v3 = connection.getHeaderField("Content-Disposition");
                if (v3 != null) {
                    v2 = v3;
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        if (v8 != null) {
            if ((v8.equalsIgnoreCase("text/plain")) || (v8.equalsIgnoreCase("application/octet-stream"))
                    ) {
                String v9 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(
                        this.mUri));
                if (v9 != null) {
                    this.mRequest.setMimeType(v9);
                }
            }

            this.mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil
                    .guessFileName(this.mUri, v2, v8));
        }

        DownloadManager v7 = (DownloadManager) this.mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        try {
            v7.enqueue(this.mRequest);
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
}

